package hu.diploma.android.serviceimpl;

import hu.diploma.common.service.StructuredNameService;
import hu.diploma.common.vos.PersonVo;
import hu.diploma.common.vos.StructuredNameVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.util.Log;

public class StructuredNameServiceImpl implements StructuredNameService {

	ContentResolver contentResolver;

	public StructuredNameServiceImpl(ContentResolver contentResolver) {
		super();
		this.contentResolver = contentResolver;
	}

	@Override
	public StructuredNameVo save(StructuredNameVo structuredNameVo) {

		ContentValues values = new ContentValues();
		values.put(Data.RAW_CONTACT_ID, structuredNameVo.getPerson().getPhoneId());
		values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
		values.put(StructuredName.DISPLAY_NAME, structuredNameVo.getDisplayName());
		values.put(StructuredName.FAMILY_NAME, structuredNameVo.getFamilName());
		values.put(StructuredName.MIDDLE_NAME, structuredNameVo.getMiddleName());
		values.put(StructuredName.GIVEN_NAME, structuredNameVo.getGivenName());
		values.put(StructuredName.PREFIX, structuredNameVo.getPreixName());

		Uri uri = this.contentResolver.insert(Data.CONTENT_URI, values);
		long id = ContentUris.parseId(uri);
		structuredNameVo.setPhoneId(id);

		return structuredNameVo;

	}

	@Override
	public StructuredNameVo merge(StructuredNameVo structuredNameVo) {

		ContentValues values = new ContentValues();
		values.put(Data.RAW_CONTACT_ID, structuredNameVo.getPerson().getPhoneId());
		values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
		values.put(StructuredName.DISPLAY_NAME, structuredNameVo.getDisplayName());
		values.put(StructuredName.FAMILY_NAME, structuredNameVo.getFamilName());
		values.put(StructuredName.MIDDLE_NAME, structuredNameVo.getMiddleName());
		values.put(StructuredName.GIVEN_NAME, structuredNameVo.getGivenName());
		values.put(StructuredName.PREFIX, structuredNameVo.getPreixName());

		if (structuredNameVo.getPhoneId() != null) {
			this.contentResolver.update(Data.CONTENT_URI, values, BaseColumns._ID + "=?", new String[] { structuredNameVo.getPhoneId().toString() });

		} else {

			Uri uri = this.contentResolver.insert(Data.CONTENT_URI, values);
			long id = ContentUris.parseId(uri);
			structuredNameVo.setPhoneId(id);
		}
		return structuredNameVo;

	}

	@Override
	public StructuredNameVo findById(Serializable searchId) {
		Log.d(this.getClass().getCanonicalName(), "Start: loadStructuredName ");
		StructuredNameVo structuredName = new StructuredNameVo();
		String where = BaseColumns._ID + " = ? AND " + Data.MIMETYPE + " = ?";
		String[] whereParameters = new String[] { searchId.toString(), StructuredName.CONTENT_ITEM_TYPE };
		Cursor cursor = this.contentResolver.query(Data.CONTENT_URI, null, where, whereParameters, null);
		try {
			if (cursor.moveToFirst()) {
				Integer id = cursor.getInt(cursor.getColumnIndex(BaseColumns._ID));
				String displayName = cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME));
				String familName = cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
				String middleName = cursor.getString(cursor.getColumnIndex(StructuredName.MIDDLE_NAME));
				String givenName = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
				String preixName = cursor.getString(cursor.getColumnIndex(StructuredName.PREFIX));

				structuredName.setId(id.longValue());
				structuredName.setDisplayName(displayName);
				structuredName.setFamilName(familName);
				structuredName.setMiddleName(middleName);
				structuredName.setGivenName(givenName);
				structuredName.setPreixName(preixName);

			}
		} finally {
			cursor.close();
		}
		Log.d(this.getClass().getCanonicalName(), "End: loadStructuredName ");
		return structuredName;
	}

	@Override
	public List<StructuredNameVo> loadAll() {
		Log.d(this.getClass().getCanonicalName(), "Start: loadStructuredName ");
		List<StructuredNameVo> structuredNameVos = new ArrayList<StructuredNameVo>();
		String where = Data.MIMETYPE + " = ?";
		String[] whereParameters = new String[] { StructuredName.CONTENT_ITEM_TYPE };
		Cursor cursor = this.contentResolver.query(Data.CONTENT_URI, null, where, whereParameters, null);

		try {
			while (cursor.moveToNext()) {

				Integer id = cursor.getInt(cursor.getColumnIndex(BaseColumns._ID));
				String displayName = cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME));
				String familName = cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
				String middleName = cursor.getString(cursor.getColumnIndex(StructuredName.MIDDLE_NAME));
				String givenName = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
				String preixName = cursor.getString(cursor.getColumnIndex(StructuredName.PREFIX));
				StructuredNameVo structuredName = new StructuredNameVo();
				structuredName.setId(id.longValue());
				structuredName.setDisplayName(displayName);
				structuredName.setFamilName(familName);
				structuredName.setMiddleName(middleName);
				structuredName.setGivenName(givenName);
				structuredName.setPreixName(preixName);
				structuredNameVos.add(structuredName);

			}
		} finally {
			cursor.close();
		}
		Log.d(this.getClass().getCanonicalName(), "End: loadStructuredName ");
		return structuredNameVos;
	}

	@Override
	public void delete(StructuredNameVo structuredNameVo) {

		this.contentResolver.delete(Data.CONTENT_URI, BaseColumns._ID + "=?", new String[] { structuredNameVo.getPhoneId().toString() });

	}

	@Override
	public StructuredNameVo findByPeople(PersonVo peopleVo) {
		Log.d(this.getClass().getCanonicalName(), "Start: loadStructuredName ");
		StructuredNameVo structuredName = new StructuredNameVo();
		String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
		String[] whereParameters = new String[] { peopleVo.getPhoneId().toString(), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
		Cursor cursor = this.contentResolver.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);
		try {
			if (cursor.moveToFirst()) {
				Integer id = cursor.getInt(cursor.getColumnIndex(BaseColumns._ID));
				String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
				String familName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
				String middleName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
				String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
				String preixName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));

				structuredName.setId(id.longValue());
				structuredName.setDisplayName(displayName);
				structuredName.setFamilName(familName);
				structuredName.setMiddleName(middleName);
				structuredName.setGivenName(givenName);
				structuredName.setPreixName(preixName);

			}
		} finally {
			cursor.close();
		}
		Log.d(this.getClass().getCanonicalName(), "End: loadStructuredName ");
		return structuredName;
	}

	@Override
	public StructuredNameVo findByPhoneId(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
